import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { renderImage, renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';
import { TreeOption } from '@/api/cardCategories';

const dict = useDictStore();

export class State {
  public id = 0; // 商品ID
  public title = ''; // 商品标题
  public image = ''; // 封面图
  public shopId = null; // 所属店铺
  public categoryId = null; // 分类
  public originalPrice = null; // 原价
  public currentPrice = null; // 现价
  public discount = ''; // 折扣
  public isHot = null; // 热销
  public rating = 0; // 评分
  public reviews = 0; // 评价数
  public promotionCount = 0; // 促销库存
  public soldCount = 0; // 已售数量
  public memberCashback = 0; // 会员返现
  public referralCommission = 0; // 推荐佣金
  public validityPeriod = ''; // 有效期
  public refundPolicy = ''; // 退款政策
  public usageRules = ''; // 使用规则
  public description = ''; // 商品描述
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 更新时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  title: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入商品标题',
  },
  shopId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入所属店铺',
  },
  categoryId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入分类',
  },
  originalPrice: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入原价',
  },
  currentPrice: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入现价',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'title',
    component: 'NInput',
    label: '商品标题',
    componentProps: {
      placeholder: '请输入商品标题',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '商品ID',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '商品标题',
    key: 'title',
    align: 'left',
    width: -1,
  },
  {
    title: '封面图',
    key: 'image',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderImage(row.image);
    },
  },
  {
    title: '所属店铺',
    key: 'shopId',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('cardShopOption', row.shopId);
    },
  },
  {
    title: '原价',
    key: 'originalPrice',
    align: 'left',
    width: -1,
  },
  {
    title: '现价',
    key: 'currentPrice',
    align: 'left',
    width: -1,
  },
  {
    title: '热销',
    key: 'isHot',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('sys_normal_disable', row.isHot);
    },
  },
  {
    title: '促销库存',
    key: 'promotionCount',
    align: 'left',
    width: -1,
  },
  {
    title: '已售数量',
    key: 'soldCount',
    align: 'left',
    width: -1,
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: -1,
  },
  {
    title: '更新时间',
    key: 'updatedAt',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_normal_disable', 'cardShopOption', 'cardCategoriesOption']);
}

// 关系树选项
export const treeOption = ref([]);

// 加载关系树选项
export function loadTreeOption() {
  TreeOption().then((res) => {
    treeOption.value = res;
  });
}